package com.beasu.tingting.services

import android.app.Service
import android.content.Intent
import android.media.MediaPlayer
import android.os.Binder
import android.os.IBinder
import android.util.Log
class MusicService  : Service() {
   private var mBinder: MusicBinder  ? = null
   private  lateinit  var player: MediaPlayer
    override fun onBind(intent: Intent?): IBinder? {
        Log.i("服务","Bind")
        if(mBinder == null){
            mBinder = MusicBinder()
        }
        return mBinder
    }
    override fun onDestroy() {
        super.onDestroy()
        Log.i("服务","onDestroy")
        player.release()
    }
    override fun onRebind(intent: Intent?) {
        Log.i("服务","ReBind")
        super.onRebind(intent)
    }
    override fun onCreate() {
        super.onCreate()
        mBinder = MusicBinder()
        player = MediaPlayer()
        player.setDataSource("/storage/emulated/0/Music/落雪三千.mp3")
        player.prepare()
    }
   inner class MusicBinder : Binder() {
            fun isPlaying():Boolean{
                return player.isPlaying
            }
            fun play(){
                if(player.isPlaying){
                    player.pause()
                }else{
                    player.start()
                }
                Log.i("服务","播放音乐")
            }
         fun currentPosition():Int{
             return player.currentPosition
         }
       fun seekTo( mesc:Int){
        player.seekTo(mesc)
       }
       fun stop(){
           player.stop()
       }
    }
}
